import { createRouter, createWebHashHistory,createWebHistory, RouteRecordRaw } from 'vue-router'
import { staticRoutes } from './constantRoutes'


const router = createRouter({
  history: createWebHistory(),
  routes: staticRoutes as RouteRecordRaw[],
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
})


//获取token数据
import userPinia from '@/store/modules/user/userStore';

//设置路由守卫,检查userPinia token是否为null
//后台登录保护拦截器
router.beforeEach((to,from,next)=>{
  let userStore = userPinia();
  console.log(userStore.token);
   if(userStore.token){
     //登录
     //特殊: 登录还想去/login
     if(to.path == '/login'){
        next("/");//去首页
     }
     next();
   }else{
     
    if(to.path == "/login"){
      next(); //不在触发守卫,直接去登录页面
    }
     //等于 null  没有登录
     next("/login")
   }
})


export default router
